﻿using System.Web.Mvc;
using SeaBattle.UI.Web.Models;
using System.Web.Security;
using SeaBattle.Domain.Abstract;
using System.Linq;

namespace SeaBattle.UI.Web.Controllers
{
    public class HomeController : Controller
    {
        private IPlayerRepository playerRepository;
        public HomeController(IPlayerRepository playerRepository)
        {
            this.playerRepository = playerRepository;
        }
        //
        // GET: /Home/

        public ActionResult StartProject()
        {
            return View();
        }

        public ActionResult LogOn()
        {
            return View(new LogOnModel());
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model)
        {            
            if (ModelState.IsValid)
            {
                var players = playerRepository.Players;
                if (players.FirstOrDefault(x=>(x.Login==model.Login && 
                    x.Password == model.Password)) != null)
                {
                    FormsAuthentication.SetAuthCookie(model.Login, false);
                    return RedirectToAction("Index", "Room");
                }
                ModelState.AddModelError("","Incorrect Login or Password");
            }
            return View(model);
        }

        public ActionResult Exit()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("LogOn");
        }
    }
}
